#!/usr/bin/python
# -*- coding: utf-8 -*-


import ctypes
import struct
import os
import _winreg

"""
Windows 开发环境
电脑重新安装后，许多开发环境需要重新设置，每次都要网上搜索，
这里将工具的配置环境都记录下来
下次重要安装后，一键配置
主要功能：
1、添加路径到环境变量
2、从环境变量删除路径
3、一键全部设置
4、永久和临时设置

慢慢修改吧 
# 调用python 的_winreg 模块，设置临时或者永久的PATH 环境变量


"""

# 系统环境变量--"SYSTEM"
# HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment
# User 环境变量--"USER"
# HKEY_CURRENT_USER\Environment

EnvType = "SYSTEM"  # value can be "SYSTEM"  or "USER"

# name            工具的名称
# description     工具功能简介及描述
# homepage        工具主页
# downloadurl     安装包下载url
# root            工具的安装路径(绝对)

# env_list  其他扩展的环境变量列表
#          operate      'create' or 'append' (默认为append)
#          env_name       环境变量名称，可以为任意
#          env_value      相对路径
Config = [
    dict(
        name="git",
        description=ur"TortoiseGit 是 Git Windows 客户端",
        homepage=ur"https://tortoisegit.org/",
        downloadurl="",
        root=ur'C:\Program Files\Git',
        env_list=[
            dict(operate="append", env_name="PATH", env_value="bin")
        ]
    ),
    dict(
        name='svn',
        description=ur"TortoiseSVN 是 SVN Windows 客户端",
        homepage=ur"https://tortoisesvn.net/",
        downloadurl="",
        root=ur'C:\\Program Files\\TortoiseSVN',
        env_list=[
            dict(operate='append', env_name="PATH", env_value='bin'),
        ]
    ),
    dict(
        name='eclipse',
        description=ur"www.baidu.com",
        homepage=ur"www.baidu.com",
        downloadurl="",
        root=ur'D:\Android\adt-bundle-windows-x86-20140702\eclipse',
        env_list=[
            dict(operate='append', env_name="PATH", env_value=''),
        ]
    ),
    dict(
        name='android',
        description="Android sdk and nkd directory  adb tools etc",
        homepage="www.baidu.com",
        downloadurl="",
        root=ur"D:\platform\android",
        env_list=[
            dict(operate="append", env_name="PATH", env_value="sdk"),
            dict(operate='append', env_name="PATH", env_value="ndk"),
            dict(operate='append', env_name="PATH", env_value=ur"sdk\platform-tools"),
        ]
    ),
    dict(
        name='qt',
        description="www.baidu.com",
        homepage="www.baidu.com",
        downloadurl="",
        root=ur'D:\Qt',
        env_list=[
            dict(operate='create', env_name="QTDIR", env_value=''),
            dict(operate='append', env_name="PATH", env_value=ur'4.8.6\bin'),
            dict(operate='create', env_name="QMAKESPEC", env_value=ur'4.8.6\mkspecs\win32-g++')
        ]
    ),

    dict(
        name='base',  # ----include lua premake4 etc
        description=ur"www.baidu.com",
        homepage=ur"www.baidu.com",
        downloadurl="",
        root=ur'D:\platform\base',
        env_list=[
            dict(operate='append', env_name="PATH", env_value=''),
            dict(operate='append', env_name="PATH", env_value='gow'),
            dict(operate='append', env_name="PATH", env_value='msys'),
            dict(operate='append', env_name="PATH", env_value='msys2')
        ]
    ),
    dict(
        name='lua',
        description=ur"www.baidu.com",
        homepage=ur"www.baidu.com",
        downloadurl="",
        root=ur'D:\platform\lua',
        env_list=[
            dict(operate='append', env_name="PATH", env_value='bin'),
            dict(operate='append', env_name="LIBRARY_PATH", env_value='lib'),
            dict(operate='append', env_name="C_INCLUDE_PATH", env_value='include')
        ]
    ),

    dict(
        name='swig',
        description=ur"www.baidu.com",
        homepage=ur"www.baidu.com",
        downloadurl="",
        root=ur'D:\platform\swigwin-3.0.7',
        env_list=[
            dict(operate='append', env_name="PATH", env_value=''),
        ]
    ),
    dict(
        name='gradle',
        description=ur"www.baidu.com",
        homepage=ur"www.baidu.com",
        downloadurl="",
        root=ur'D:\Android\AndroidStudio\gradle\gradle-2.10',
        env_list=[
            dict(operate='append', env_name="PATH", env_value='bin'),
        ]
    ),

    dict(
        name='cmake',
        description=ur"www.baidu.com",
        homepage=ur"www.baidu.com",
        downloadurl="",
        root=ur'D:\platform\cmake',
        env_list=[
            dict(operate='append', env_name="PATH", env_value='bin'),
        ]
    ),

    dict(
        name='doxy',
        description=ur"www.baidu.com",
        homepage=ur"www.baidu.com",
        downloadurl="",
        root=ur'D:\platform\doxygen',
        env_list=[
            dict(operate='append', env_name="PATH", env_value='bin'),
        ]
    ),
    dict(
        name='luapower',
        description=ur"www.baidu.com",
        homepage=ur"www.baidu.com",
        downloadurl="",
        root=ur'D:\platform\luapower',
        env_list=[
            dict(operate='append', env_name="PATH", env_value=''),
        ]
    ),

    dict(
        name='dmd',
        description="www.baidu.com",
        homepage="www.baidu.com",
        downloadurl="",
        root=ur'D:\platform\dmd',
        env_list=[
            dict(operate='append', env_name="PATH", env_value=ur'windows\bin'),
            dict(operate='append', env_name="PATH", env_value=ur'windows\lib'),
        ]
    ),

    dict(
        name='mingw32',
        description="www.baidu.com",
        homepage="www.baidu.com",
        downloadurl="",
        root=ur'D:\platform\mingw32',
        env_list=[
            dict(operate='append', env_name="PATH", env_value='bin'),
            dict(operate='append', env_name="PATH", env_value=ur'libexec\gcc\i686-w64-mingw32\4.8.2'),
            dict(operate='append', env_name="LIBRARY_PATH", env_value='lib'),
            dict(operate='append', env_name="LIBRARY_PATH", env_value=ur'lib\gcc\i686-w64-mingw32\4.8.2'),
            dict(operate='append', env_name="C_INCLUDE_PATH", env_value='include'),
            dict(operate='append', env_name="C_INCLUDE_PATH", env_value=ur'lib\gcc\i686-w64-mingw32\4.8.2\\include'),
            dict(operate='append', env_name="CPLUS_INCLUDE_PATH", env_value=ur'i686-w64-mingw32\include'),
            dict(operate='append', env_name="CPLUS_INCLUDE_PATH", env_value=ur'i686-w64-mingw32\include\c++'),
            dict(operate='append', env_name="CPLUS_INCLUDE_PATH", env_value=ur'i686-w64-mingw32\include\c++\backward'),

        ]
    ),

    dict(
        name='java',
        description="www.baidu.com",
        homepage="www.baidu.com",
        downloadurl="",
        root=ur'D:\platform\jdk1.8.0_60',
        env_list=[
            dict(operate='create', env_name="JAVA_HOME", env_value=''),
            dict(operate='append', env_name="PATH", env_value='bin'),
            dict(operate='append', env_name="PATH", env_value='lib'),
            dict(operate='append', env_name="C_INCLUDE_PATH", env_value='include'),
            dict(operate='append', env_name="C_INCLUDE_PATH", env_value=ur'include\win32'),
            dict(operate='append', env_name="CPLUS_INCLUDE_PATH", env_value='include'),
            dict(operate='append', env_name="CPLUS_INCLUDE_PATH", env_value=ur'include\win32'),
        ]
    ),

    dict(
        name='python',
        description=ur"www.baidu.com",
        homepage=ur"www.baidu.com",
        downloadurl="",
        root=ur'D:\platform\Python27',
        env_list=[
            dict(operate='create', env_name="PYTHONPATH", env_value=''),
            dict(operate='append', env_name="PATH", env_value=''),
            dict(operate='append', env_name="PATH", env_value='Scripts'),
            dict(operate='append', env_name="PATH", env_value='DLLs'),
            # dict(flag='default',env_name = "PATH",env_value ='Lib\\site-packages\\PyQt4'),
            dict(operate='append', env_name="C_INCLUDE_PATH", env_value='include'),
            dict(operate='append', env_name="CPLUS_INCLUDE_PATH", env_value='include'),
            dict(operate='append', env_name="LIBRARY_PATH", env_value='libs'),
            dict(operate='append', env_name="LD_LIBRARY_PATH", env_value='libs'),

        ]
    ),
    dict(
        name='npm',
        description=ur"www.baidu.com",
        homepage=ur"www.baidu.com",
        downloadurl="",
        root=ur'D:\platform\node-v6.11.3-win-x86',
        env_list=[
            dict(operate='append', env_name="PATH", env_value=''),
        ]
    ),
    dict(
        name='xmake',
        description=ur"一个基于lua的跨平台自动构建工具(类似cmake/premake",
        homepage=ur"http://xmake.io/",
        downloadurl=ur"https://github.com/tboox/xmake/releases",
        root=ur'D:\platform\xmake',
        env_list=[
            dict(operate='append', env_name="PATH", env_value=''),
        ]
    ),
]


def download_tool(url):
    pass


def download_all():
    pass


def show_one_item():
    pass


def show_all_item():
    pass


def show_menu():
    pass


def get_key_value(env_name):
    """
    查询环境变量的值
    :param env_name:环境变量名 例如"PATH"
    :return:
    """
    if EnvType == "SYSTEM":
        root = _winreg.HKEY_LOCAL_MACHINE
        sub_key = r"SYSTEM\ControlSet001\Control\Session Manager\Environment"
    else:
        root = _winreg.HKEY_CURRENT_USER
        sub_key = r"Environment"

    access_rights = _winreg.KEY_ALL_ACCESS
    with _winreg.OpenKey(root, sub_key, 0, access_rights) as hkey:
        return _winreg.QueryValueEx(hkey, env_name)

    handler = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r"SYSTEM\ControlSet001\Control\Session Manager\Environment", 0,
                              _winreg.KEY_ALL_ACCESS)


def set_key_value(env_name, env_value):
    """
    设置注册表环境变量值
    :param env_name: 环境变量名 例如"PATH"
    :param env_value: 环境变量值
    :return:
    """
    if EnvType == "SYSTEM":
        root = _winreg.HKEY_LOCAL_MACHINE
        sub_key = r"SYSTEM\ControlSet001\Control\Session Manager\Environment"
    else:
        root = _winreg.HKEY_CURRENT_USER
        sub_key = r"Environment"

    access_rights = _winreg.KEY_ALL_ACCESS
    with _winreg.OpenKey(root, sub_key, 0, access_rights) as hkey:
        return _winreg.SetValueEx(hkey, env_name, 0, _winreg.REG_SZ, env_value)


"""
Main function
"""

if __name__ == '__main__':
    get_key_value("PATH")
    pass
